
#include "TFRuleCheckerImpl.h"

TFRuleCheckerImpl::TFRuleCheckerImpl()
{
}

TFRuleCheckerImpl::~TFRuleCheckerImpl()
{
}

HRESULT TFRuleCheckerImpl::CheckGame()
{
	return E_FAIL;
}

bool TFRuleCheckerImpl::LegalMove(TFBoard *iCurrentBoard, 
								  TFGameTurn* myTurn, 
								  bool spendPoints){
	int x1 = myTurn->GetMove()->GetX1();
	int y1 = myTurn->GetMove()->GetY1();
	int x2 = myTurn->GetMove()->GetX2();
	int y2 = myTurn->GetMove()->GetY2();

	int dx = abs(x1 - x2);
	int dy = abs(y1 - y2);

	//Ensure that the piece is only being moved 1 square.
	if((dx + dy) != 1){
		return false;
	}

	TFGamePiece* myPiece = /*_model->GetBoard()*/iCurrentBoard->GetPieceAt(x1, y1);
	TFGamePiece* gamePiece = /*_model->GetBoard()*/iCurrentBoard->GetPieceAt(x2, y2);
	int movePoints = myTurn->GetPoints();
	int pieceMovePoints = myPiece->GetPoints();

	bool sameColor = false;
	if(gamePiece->GetColor() == myPiece->GetColor()){
		sameColor = true;
	}

	if(movePoints > 0 )
	{
		if(myPiece->GetPiece() == TROOP_PIECE){
			if(pieceMovePoints != 0){
				switch(gamePiece->GetPiece())
				{
				case EMPTY:
					{						
						if(spendPoints)
						{
							myTurn->UsePoints(1);
							myPiece->UsePoint();
						}
						return true;
					}

				case TROOP_PIECE:
					{
						if(sameColor)
						{
							//Commented out by Priyesh, 12/10 2:14am
							//I think this is what Rob meant to remove 
							/*if(movePoints >= POINTS_TO_MOVE_THROUGH_YOUR_TROOP){
								if(spendPoints)
								{
									myTurn->UsePoints(POINTS_TO_MOVE_THROUGH_YOUR_TROOP);
									myPiece->UsePoint();
								}
								return true;
							}*/
						}else
						{
							if(movePoints >= POINTS_TO_CAPTURE_TROOP){	
								if(spendPoints)
								{
									myTurn->UsePoints(POINTS_TO_CAPTURE_TROOP);		
									myPiece->UsePoint();
								}
								return true;
							}
						}

						break;
					}
				case FLAG_0:
					{
						if(sameColor){
							//Returns false because you can't capture your own flag.
						}else{
							if(movePoints >= POINTS_TO_CAPTURE_FLAG){
								if(spendPoints)
								{
									myTurn->UsePoints(POINTS_TO_CAPTURE_FLAG);		
									myPiece->UsePoint();	
								}
								return true;
							}
						}

						break;
					}
				case FLAG_1:
					{
						if(sameColor){
							//Returns false because you can't capture your own flag.
						}else{
							if(movePoints >= (POINTS_TO_CAPTURE_FLAG + POINTS_TO_CAPTURE_FLAG_BARRICADE)){
								if(spendPoints)
								{
									myTurn->UsePoints(POINTS_TO_CAPTURE_FLAG + POINTS_TO_CAPTURE_FLAG_BARRICADE);		
									myPiece->UsePoint();
								}
								return true;
							}
						}

						break;
					}
				case FLAG_2:
					{
						if(sameColor){
							//Returns false because you can't capture your own flag.
						}else{
							if(movePoints >= (POINTS_TO_CAPTURE_FLAG + (POINTS_TO_CAPTURE_FLAG_BARRICADE * 2))){
								if(spendPoints)
								{
									myTurn->UsePoints(POINTS_TO_CAPTURE_FLAG + (POINTS_TO_CAPTURE_FLAG_BARRICADE * 2));		
									myPiece->UsePoint();	
								}
								return true;
							}
						}

						break;

					}

				case BARRICADE_1:
					{
						if(sameColor){
							//No movement point use, no troop movement point use.
							/* Commented out by Rob, 2:32pm, Nov 20th
						   return false so that we don't move through troops
						   this is because of a bug in the model which only allows one piece
						   in a board position, it will be fixed, then this can be uncommented
						   
						   return true;
						   */
							return true;
							//Put back by Priyesh, 12/10 1:51am.. we should move through our own barricades!
						}else{
							if(movePoints >= POINTS_TO_CAPTURE_BARRICADE){
								if(spendPoints)
								{
									myTurn->UsePoints(POINTS_TO_CAPTURE_BARRICADE);		
									myPiece->UsePoint();	
								}
								return true;
							}
						}

						break;

					}
				case BARRICADE_2:
					{
						if(sameColor){
							//No movement point use, no troop movement point use.
							/* Commented out by Rob, 2:32pm, Nov 20th
						   return false so that we don't move through troops
						   this is because of a bug in the model which only allows one piece
						   in a board position, it will be fixed, then this can be uncommented
						   
						   return true;
						   */
							return true;
							//Put back by Priyesh, 12/10 1:51am.. we should move through our own barricades!
						}else{
							if(movePoints >= (POINTS_TO_CAPTURE_BARRICADE * 2)){
								if(spendPoints)
								{
									myTurn->UsePoints(POINTS_TO_CAPTURE_BARRICADE*2);		
									myPiece->UsePoint();
								}
								return true;
							}
						}

						break;

					}
				case BARRICADE_3:
					{
						if(sameColor){
							//No movement point use, no troop movement point use.
							/* Commented out by Rob, 2:32pm, Nov 20th
						   return false so that we don't move through troops
						   this is because of a bug in the model which only allows one piece
						   in a board position, it will be fixed, then this can be uncommented
						   
						   return true;
						   */
							return true;
							//Put back by Priyesh, 12/10 1:51am.. we should move through our own barricades!
						}else{
							if(movePoints >= (POINTS_TO_CAPTURE_BARRICADE*3)){
								if(spendPoints)
								{
									myTurn->UsePoints(POINTS_TO_CAPTURE_BARRICADE*3);		
									myPiece->UsePoint();	
								}
								return true;
							}
						}
						
						break;

					}
				case TF_PIECE_STATE_NULL:
					{
						break;
					}
				}				
			}
		}
	}

	return false;
}



/** Returns true if the points for the turn are even and player receives a barricade. */
bool TFRuleCheckerImpl::CanPlaceBarricade(int numPointsThisTurn){
	if(numPointsThisTurn % 2 == 0){
		return 1;
	}

	return 0;
}

/** Returns true if the points for the turn are high enough to receives a flag barricade. */
bool TFRuleCheckerImpl::CanPlaceFlagFortification(int numPointsThisTurn){
	if(numPointsThisTurn % 6 == 0){
		return 1;
	}

	return 0;
}

/** Returns the cost to move to this next position. */
int TFRuleCheckerImpl::CostToCapture(TFGamePiece *iPiece, int yourColor){
	int cost = 1;

	switch(iPiece->GetPiece()){
		case EMPTY:
			{
				cost = 1;
				break;
			}
		case FLAG_0:
			{
				if(yourColor == iPiece->GetColor()){
					cost = 999;
				}else{
					cost = 4;
				}
				break;
			}
		case FLAG_1:
			{
				if(yourColor == iPiece->GetColor()){
					cost = 999;
				}else{
					cost = 5;
				}
				break;
			}
		case FLAG_2:
			{
				if(yourColor == iPiece->GetColor()){
					cost = 999;
				}else{
					cost = 6;
				}
				break;
			}
		case BARRICADE_1:
			{
				if(yourColor == iPiece->GetColor()){
					cost = 0;
				}else{
					cost = 2;
				}
				break;
			}
		case BARRICADE_2:
			{
				if(yourColor == iPiece->GetColor()){
					cost = 0;
				}else{
					cost = 4;
				}
				break;
			}
		case BARRICADE_3:
			{
				if(yourColor == iPiece->GetColor()){
					cost = 0;
				}else{
					cost = 6;
				}
				break;
			}
		case TROOP_PIECE:
			{
				if(yourColor == iPiece->GetColor()){
					cost = 2;
				}else{
					cost = 5;
				}
				break;

			}
		case TF_PIECE_STATE_NULL:
			{
					break;
			}
	}

	return cost;
}

/** Checks to see if the place you want to put a piece is legal. */
bool TFRuleCheckerImpl::CanPlacePieceHere(int row, int col,TF_ADD_PIECE piece, TF_PLAYERS color, TFBoard *iCurrentBoard){


	TF_PIECE_STATE dest_piece = iCurrentBoard->GetPieceAt(col,row)->GetPiece();

	switch(piece){
		case BARRICADE:
			{
				if(dest_piece == EMPTY || dest_piece == BARRICADE_1 ||
					dest_piece == BARRICADE_2){
					
					if(color == BLUE_PLAYER){
						if(row > 3 ){
							return true;
						}else{
							return false;
						}
					}else{
						if(row <= 3 ){
							return true;
						}else{
							return false;
						}
					}
				}else{
					return false;
				}
				break;
			}
		case TROOP_TEMP:
			{
				if(dest_piece == EMPTY){
					if(color == BLUE_PLAYER){
						if(row == 6 || row == 7){
							return true;
						}else{
							return false;
						}
					}
					else if(color == RED_PLAYER)
					{
						if(row == 1 || row == 0){
							return true;
						}else{
							return false;
						}
					}
				}else{
					return false;
				}
				break;
			}
		case FLAG:
			{
				if(dest_piece == EMPTY){
					if(color == BLUE_PLAYER){
						if(row == 7 ){
							return true;
						}else{
							return false;
						}
					}
					else if(color == RED_PLAYER)
					{
						if(row == 0){
							return true;
						}else{
							return false;
						}
					}
				}else{
					return false;
				}
				break;
			}
		case FLAG_FORTIFICATION:
			{
				if(dest_piece == FLAG_0 || dest_piece == FLAG_1){
					if ((color == BLUE_PLAYER && row == 7) ||
						(color == RED_PLAYER && row == 0))
					{
						return true;
					}
				}else{
					return false;
				}
				break;
			}
		case TF_ADD_PIECE_NULL:
			{
				return false;	
			}
	}

	return false;
}
